package com.adinnet.admin.system.service.impl;

import com.adinnet.admin.common.service.CommonService;
import com.adinnet.admin.organization.mapper.OrganizationMapper;
import com.adinnet.admin.organization.model.Organization;
import com.adinnet.admin.school.mapper.SchoolMapper;
import com.adinnet.admin.school.model.School;
import com.adinnet.admin.system.model.vo.SysUserInfo;
import com.adinnet.admin.system.model.vo.SysUserRoleVo;
import com.adinnet.admin.system.model.vo.SysUserVo;
import com.adinnet.admin.system.mapper.SysUserMapper;
import com.adinnet.admin.system.mapper.SysUserRoleMapper;
import com.adinnet.admin.system.model.SysRole;
import com.adinnet.admin.system.model.SysUser;
import com.adinnet.admin.system.model.SysUserRole;
import com.adinnet.admin.system.service.SysUserService;

import com.adinnet.common.biz.enums.RoleCode;
import com.adinnet.core.JsonResult;
import com.adinnet.core.PageResult;
import com.adinnet.core.exception.BizException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.converters.DateConverter;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author sunjianbo
 * @since 2019-05-17
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {

    @Autowired
    private SysUserRoleMapper sysUserRoleMapper;

    @Autowired
    private OrganizationMapper organizationMapper;
    @Autowired
    private SchoolMapper schoolMapper;
    @Autowired
    private CommonService commonService;

    @Override
    public SysUser getByUsername(String username) {
        return baseMapper.selectByUsername(username);
    }

    @Override
    public PageResult<SysUserVo> listUser(Integer page, Integer limit, String searchKey, String searchValue,Integer roleId) {
        if (page == null || limit == null) {
            page = 1;
            limit = 10;
        }
        Page<SysUserVo> objectPage = new Page<>(page, limit);
        List<SysUserVo> userList = getUserList(objectPage, searchKey, searchValue, roleId);
        return new PageResult<>(userList, objectPage.getTotal());
    }

    /**
     * @param objectPage:
     * @param searchKey:
     * @param searchValue:
     * @param roleId:
     * @Description: 用户列表
     * @author zYshuai
     * @Date: 2020/12/24 17:52
     * @return: java.util.List<com.adinnet.admin.system.model.vo.SysUserVo>
     */
    List<SysUserVo> getUserList(Page<SysUserVo> objectPage , String searchKey, String searchValue, Integer roleId) {
        return baseMapper.getUserList(objectPage, searchKey, searchValue, roleId);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean add(SysUser user, List<Integer> roleIds) {
        if (baseMapper.selectByUsername(user.getUserName()) != null) {
            throw new BizException("账号已经存在");
        }
        boolean rs = baseMapper.insert(user) > 0;
        if (rs) {
            if (sysUserRoleMapper.insertBatch(user.getId(), roleIds) < roleIds.size()) {
                throw new BizException("添加失败，请重试");
            }
        }
        return rs;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean update(SysUser user, List<Integer> roleIds) {
        user.setUserName(null);  // 账号不能修改
        boolean rs = baseMapper.updateById(user) > 0;
        if (rs) {
            sysUserRoleMapper.delete(new UpdateWrapper<SysUserRole>().eq("user_id", user.getId()));
            if (sysUserRoleMapper.insertBatch(user.getId(), roleIds) < roleIds.size()) {
                throw new BizException("修改失败，请重试");
            }
        }
        return rs;
    }



    // 获取用户id
    private List<Integer> getUserIds(List<SysUser> userList) {
        List<Integer> userIds = new ArrayList<>();
        for (SysUser sysUser : userList) {
            userIds.add(sysUser.getId());
        }
        return userIds;
    }

    /**
     * @Description: 查询机构下所有人
     * @author zYshuai
     * @Date: 2020/12/14 13:31
    * @param organizationId:
    * @return: com.adinnet.core.JsonResult
     */
    @Override
    public List<SysUser> getListByOrganizationId(Integer organizationId) {
        return baseMapper.selectList(new QueryWrapper<SysUser>().eq("deleted", 0).eq("organization_id", organizationId));
    }

}
